<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="样品id" prop="sampleId">
         <el-input
          v-model="queryParams.sampleId"
          placeholder="请输入样品id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="样品名称" prop="sampleName">
        <el-input v-model="queryParams.sampleName" placeholder="请输入样品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="样品状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择样品状态">
          <el-option v-for="dict in status" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="供应商id" prop="supplierId">
        <el-input
          v-model="queryParams.supplierId"
          placeholder="请输入供应商id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="检验结果" prop="checkResult">
        <el-input
          v-model="queryParams.checkResult"
          placeholder="请输入检验结果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验详情" prop="checkInfo">
        <el-input
          v-model="queryParams.checkInfo"
          placeholder="请输入检验详情"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="快递单号" prop="trackingNumber">
        <el-input
          v-model="queryParams.trackingNumber"
          placeholder="请输入快递单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:sample:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:sample:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:sample:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:sample:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sampleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="样品id" align="center" prop="sampleId" /> -->
      <el-table-column label="样品名称" align="center" prop="sampleName" />
      <el-table-column label="供应商" align="center" prop="supplierName" />
      <el-table-column label="样品状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sample_check_status" :value="scope.row.status">
          </dict-tag>
        </template>
      </el-table-column>

      <!-- <el-table-column label="检验结果" align="center" prop="checkResult" />
      <el-table-column label="检验详情" align="center" prop="checkInfo" />
      <el-table-column label="检测报告" align="center" prop="checkFile" />
      <el-table-column label="快递单号" align="center" prop="trackingNumber" /> -->

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:sample:view']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:sample:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:sample:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改样品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="样品id" prop="sampleId">
          <el-input v-model="form.sampleId" placeholder="请输入样品id" />
        </el-form-item> -->
        <el-form-item label="样品名称" prop="sampleName">
          <el-input v-model="form.sampleName" placeholder="请输入样品名称" />
        </el-form-item>

        <!-- 供应商列表 -->
        <el-form-item label="供应商" prop="supplierId">
          <el-select v-model="form.name" filterable placeholder="请选择">
            <el-option v-for="item in supplierList" :key="item.supplierName" :value="item.supplierName"></el-option>
          </el-select>
        </el-form-item>

        <!-- 样品状态列表 -->
        <el-form-item label="样品状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择样品状态">
            <el-option v-for="dict in status" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue">
            </el-option>
          </el-select>
        </el-form-item>

        <!-- 根据样品状态选择表单 -->
        {{ form.status }}
        <!-- 快递中 -->
        <el-form-item label="填写快递单号" v-if="form.status == '1'">
          <input type="text">
        </el-form-item>
        <!-- 已收货 -->
        <el-form-item label="填写快递单号" v-if="form.status == '2'">
          <input type="text">
        </el-form-item>
        <!-- 已送检 -->
        <el-form-item label="填写快递单号" v-if="form.status == '3'">
          <input type="text">
        </el-form-item>
        <!-- 已检验 -->
        <el-form-item label="填写快递单号" v-if="form.status == '4'">
          <input type="text">
        </el-form-item>


        <!-- <template slot-scope="scope">
            <dict-tag :options="dict.type.sample_check_status" :value="scope.row.status">
            </dict-tag>
          </template> -->
        <!-- <el-select v-model="selectedValue" placeholder="请选择">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select> -->
        <!-- <el-form-item label="供应商id" prop="supplierId">
          <el-input v-model="form.supplierId" placeholder="请输入供应商id" />
        </el-form-item>
        <el-form-item label="检验结果" prop="checkResult">
          <el-input v-model="form.checkResult" placeholder="请输入检验结果" />
        </el-form-item>
        <el-form-item label="检验详情" prop="checkInfo">
          <el-input v-model="form.checkInfo" placeholder="请输入检验详情" />
        </el-form-item> -->
        <!-- <el-form-item label="检测报告" prop="checkFile">
          <file-upload v-model="form.checkFile"/>
        </el-form-item>
        <el-form-item label="快递单号" prop="trackingNumber">
          <el-input v-model="form.trackingNumber" placeholder="请输入快递单号" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSample, getSample, delSample, addSample, updateSample, supplierSampleList } from "@/api/system/sample";

export default {
  name: "Sample",
  dicts: ['sample_check_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 样品信息表格数据
      sampleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //检验状态字典
      status: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sampleId: null,
        sampleName: null,
        supplierId: null,
        status: null,
        checkResult: null,
        checkInfo: null,
        checkFile: null,
        trackingNumber: null,
        deliveryTime: null,
        inspectionTime: null,
        name: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 供应商名字
      supplierList: [],
      // 选择的供应商名字
      newSupplierName: ''
    };
  },
  created() {
    this.getList();
    this.getDicts("sample_check_status").then((res) => {
      this.status = res.data;
    })
    this.getSupplierList()
  },
  methods: {
    // 拿到供应商列表
    getSupplierList() {
      supplierSampleList().then(res => {
        this.supplierList = res
        console.log(this.supplierList)
      })
    },
    /** 查询样品信息列表 */
    getList() {
      this.loading = true;
      listSample(this.queryParams).then(response => {
        this.sampleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sampleId: null,
        sampleName: null,
        supplierId: null,
        status: null,
        checkResult: null,
        checkInfo: null,
        checkFile: null,
        trackingNumber: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        supplierName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sampleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加样品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sampleId = row.sampleId || this.ids
      getSample(sampleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改样品信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        console.log(valid)
        if (valid) {
          if (this.form.sampleId != null) {
            updateSample(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form)
            // addSample(this.form).then(response => {
            //   this.$modal.msgSuccess("新增成功");
            //   this.open = false;
            //   this.getList();
            // });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sampleIds = row.sampleId || this.ids;
      this.$modal.confirm('是否确认删除样品信息编号为"' + sampleIds + '"的数据项？').then(function () {
        return delSample(sampleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sample/export', {
        ...this.queryParams
      }, `sample_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
